import { reactive } from "vue";
import { ElMessage } from "element-plus";
/** 自定义表单规则校验 */
export const createRules = reactive({
  name: [{ required: true, message: "礼品名称为必填项", trigger: "blur" }],
  point: [
    { required: true, message: "兑换积分为必填项", trigger: "blur" },
    {
      validator: (rule, value, callback) => {
        if (value <= 0) {
          callback(new Error("兑换积分必须大于0"));
        } else {
          callback();
        }
      },
      trigger: ["blur"]
    }
  ],
  shippingid: [
    { required: true, message: "发货方式为必填项", trigger: "blur" }
  ],
  inventory: [
    { required: true, message: "剩余数量为必填项", trigger: "blur" },
    {
      validator: (rule, value, callback) => {
        if (typeof value !== "number" || value < 0) {
          callback(new Error("剩余数量必须是非负整数"));
        }
        // 检查是否为整数
        if (!Number.isInteger(value)) {
          callback(new Error("剩余数量必须是整数"));
        }
        return callback();
      },
      trigger: ["blur"]
    }
  ],
  isUpload: [
    {
      required: true,
      validator: (rule, value, callback) => {
        if (value != 1) {
          callback(new Error("请上传图片"));
          ElMessage.error("请上传图片");
        } else {
          callback();
        }
      },
      trigger: "change"
    }
  ]
});

export const updateRules = reactive({
  point: [
    {
      validator: (rule, value, callback) => {
        if (value <= 0) {
          callback(new Error("兑换积分必须大于0"));
        } else {
          callback();
        }
      },
      trigger: ["blur"]
    }
  ],

  inventory: [
    {
      validator: (rule, value, callback) => {
        if (typeof value !== "number" || value < 0) {
          callback(new Error("剩余数量必须是非负整数"));
        }
        // 检查是否为整数
        if (!Number.isInteger(value)) {
          callback(new Error("剩余数量必须是整数"));
        }
        return callback();
      },
      trigger: ["blur"]
    }
  ]
});
